import request from 'axios'
import config from '@/config'

const service = request.create({
    baseURL:config.API_URL,
    'Content-Type': 'application/json;charset=UTF-8'
})

// 请求拦截
service.interceptors.request.use(
    config => {
        const token = localStorage.getItem("token");
        if (token){
            config.headers["Authorization"] = token;
        }
        return config;
    }
)

// 响应拦截
service.interceptors.response.use(
    response => {
        const res = response.data;
        return res;
    },
    error => {
        console.log("error:" + error);
        // 返回一个带有拒绝原因的Promise对象
        // Promise:异步处理对象
        return Promise.reject(error);
    }
)

export default service;